package org.icress.fusion.moc

class AdjustController {
    
        static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
        
        def adjustService
        def ajaxService
    
        def index = {
            redirect(action: "list", params: params)
        }
    
        def list = {
            params.max = Math.min(params.max ? params.int('max') : 100, 100)
            render view : "/moc/adjust/list", model : [adjustInstanceList: adjustService.list(params), adjustInstanceTotal: adjustService.count(params)]
        }
    
        def create = {
            def adjustInstance = new Adjust()
            adjustInstance.properties = params
            def workshop = ajaxService.workshop()

            render view : "/moc/adjust/create", model : [adjustInstance: adjustInstance, workshop : workshop]
        }
    
        def save = {
            def adjustInstance = new Adjust(params)
            
            if (adjustService.save(adjustInstance)) {
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'adjust.label', default: 'Adjust'), adjustInstance.id])}"
                redirect(action: "show", id: adjustInstance.id)
            }
            else {
                def workshop = ajaxService.workshop()

                render(view: "/moc/adjust/create", model: [adjustInstance: adjustInstance, workshop : workshop])
            }
        }
    
        def show = {
            def adjustInstance = adjustService.show(params.id)
            if (!adjustInstance) {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'adjust.label', default: 'Adjust'), params.id])}"
                redirect(action: "list")
            }
            else {
                render view : "/moc/adjust/show", model : [adjustInstance: adjustInstance]
            }
        }
    
        def edit = {
            def adjustInstance = adjustService.edit(params.id)
            if (!adjustInstance) {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'adjust.label', default: 'Adjust'), params.id])}"
                redirect(action: "list")
            }
            else {
                def workshop = ajaxService.workshop()
                render view : "/moc/adjust/edit", model :  [adjustInstance: adjustInstance, workshop : workshop]
            }
        }
    
        def update = {
            def adjustInstance = adjustService.edit(params.id)
            if (adjustInstance) {
                if (params.version) {
                    def version = params.version.toLong()
                    if (adjustInstance.version > version) {
                        
                        adjustInstance.errors.rejectValue("version", "default.optimistic.locking.failure"
                            , [message(code: 'adjust.label', default: 'Adjust')] as Object[], "Another user has updated this Adjust while you were editing")
                        def workshop = ajaxService.workshop()

                        render(view: "/moc/adjust/edit", model: [adjustInstance: adjustInstance, workshop : workshop])
                        return
                    }
                }
                adjustInstance.properties = params
                if (adjustService.update(adjustInstance)) {
                    flash.message = "${message(code: 'default.updated.message', args: [message(code: 'adjust.label', default: 'Adjust'), adjustInstance.id])}"
                    redirect(action: "show", id: adjustInstance.id)
                }
                else {
                    def workshop = ajaxService.workshop()

                    render(view: "/moc/adjust/edit", model: [adjustInstance: adjustInstance, workshop : workshop])
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'adjust.label', default: 'Adjust'), params.id])}"
                redirect(action: "list")
            }
        }
    
        def delete = {
            def adjustInstance = adjustService.edit(params.id)
            if (adjustInstance) {
                try {
                    adjustService.delete(adjustInstance)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'adjust.label', default: 'Adjust'), params.id])}"
                    redirect(action: "list")
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'adjust.label', default: 'Adjust'), params.id])}"
                    redirect(action: "show", id: params.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'adjust.label', default: 'Adjust'), params.id])}"
                redirect(action: "list")
            }
        }
    }
